Page({
  data: {
    password: ''
  },

  // 每次页面显示时清空输入框
  onShow() {
    this.setData({ password: '' })
    getApp().globalData.isAdminLoggedIn = false
  },

  onPasswordInput(e) {
    this.setData({ password: e.detail.value })
  },

  handleLogin() {
    const app = getApp()
    if (this.data.password === 'admin123') {
      // 清空输入框
      this.setData({ password: '' }, () => {
        app.globalData.isAdminLoggedIn = true
        wx.navigateTo({
          url: '/pages/admin/index',
          success: () => {
            // 强制清空页面栈中的B页面数据
            const pages = getCurrentPages()
            if (pages.length > 1) {
              pages[pages.length - 2].setData({ password: '' })
            }
          }
        })
      })
    } else {
      wx.showToast({ title: '密码错误', icon: 'none' })
      this.setData({ password: '' }) // 清空错误输入
    }
  }
})